from Case.python_ui.Common.basepage import Basepage
from selenium.common.exceptions import StaleElementReferenceException
import time,pytest
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.PageLocate.usergroup_locator import UserGroupLocator
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.Test_data.usergroup_data import UserGroup
from Case.python_ui.Test_data.common_data import Commondata

class UserGroupPage(Basepage):


    #进入usergroup页面
    def  enter_the_usergroup_page(self,cmdopt):
        doc='进入用户组页面'
        time.sleep(5)
        for i in range(3):
            try:
                self.wait_elevisble(OrganizationLocator.user_management_tab,doc)
                self.move_mouse_to_click_element(OrganizationLocator.user_management_tab,doc)
                time.sleep(5)
                self.wait_elevisble(UserGroupLocator.user_group_tab,doc)
                self.move_mouse_to_click_element(UserGroupLocator.user_group_tab,doc)
                break
            except:
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                time.sleep(5)
                continue
        self.wait_elevisble(UserGroupLocator.user_group_title,doc)
        self.refresh_search()


    # 重制查询条件
    def refresh_search(self):
        doc = '重置筛查询条件'
        self.wait_elevisble(UserGroupLocator.user_group_search_input, doc)
        self.double_click(UserGroupLocator.user_group_search_input, doc)
        self.keyboard_event_all_select(UserGroupLocator.user_group_search_input, doc)
        self.keyboard_event(UserGroupLocator.user_group_search_input, doc)
        time.sleep(1)
        self.click_query_button()
        time.sleep(2)

    def get_action_list(self,usergroup):
        doc='获取usergroup下用户操作的列表'
        for i in range(3):
            try:
                time.sleep(2)
                self.wait_elevisble(UserGroupLocator.action_list,doc)
                break
            except:
                self.save_screen(doc+'-进入异常前截图')
                self.driver.refresh()
                self.query_user_group(usergroup)



    #创建user group
    def create_user_group(self,cmdopt):
        doc='创建用户组'
        self.enter_the_usergroup_page(cmdopt)
        time.sleep(2)
        self.wait_elevisble(UserGroupLocator.add_user_group,doc)
        self.click_element(UserGroupLocator.add_user_group,doc)
        create_user_groupname = CommonTool.generate_string()
        print(create_user_groupname)
        time.sleep(2)
        self.save_screen(doc+'-打开了新建用户组页面')
        self.wait_elevisble(UserGroupLocator.user_group_name,doc)
        self.send_keys(UserGroupLocator.user_group_name, create_user_groupname,doc)
        time.sleep(3)
        self.wait_elevisble(UserGroupLocator.user_select,doc)
        self.click_element(UserGroupLocator.user_select,doc)
        self.wait_elevisble(UserGroupLocator.chooice_user,doc)
        self.click_element(UserGroupLocator.chooice_user,doc)
        time.sleep(2)
        self.click_element(UserGroupLocator.add_user_group_ok,doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)
        self.query_user_group(create_user_groupname)
        self.click_query_button()
        search_result=self.get_user_group_text()
        return  create_user_groupname ,search_result



    #查询usergroup
    def query_user_group(self,usergroup):
        doc='查询用户组'
        self.refresh_search()
        # self.wait_elevisble(UserGroupLocator.user_group_search_input,doc)
        # self.keyboard_event(UserGroupLocator.user_group_search_input,doc)
        time.sleep(2)
        self.send_keys(UserGroupLocator.user_group_search_input,usergroup,doc)
        time.sleep(3)
        self.click_query_button()
        time.sleep(3)


    #点击查询按钮
    def click_query_button(self):
        doc='点击查询按钮'
        self.wait_elevisble(UserGroupLocator.user_group_search_button,doc)
        self.click_element(UserGroupLocator.user_group_search_button,doc)
        time.sleep(3)


    #获取查询出来的usergroup的值
    def get_user_group_text(self):
        doc='获取用户组名称'
        self.wait_elevisble(UserGroupLocator.user_group_text,doc)
        search_result=self.get_element(UserGroupLocator.user_group_text,doc).text
        return search_result

    #获取查询出来的删除usergroup的值
    def get_delet_user_group_text(self):
        doc='获取查询出来的删除usergroup的值'
        search_result=self.get_elements(UserGroupLocator.user_group_text,doc)
        return search_result

    #删除创建的usergroup
    def  delete_user_group(self,usergroup,cmdopt):
        doc='删除创建的usergroup'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_usergroup_page(cmdopt)
        self.query_user_group(usergroup)
        self.get_action_list(usergroup)
        time.sleep(2)
        #self.move_mouse_to_click_element(UserGroupLocator.action_list,doc)
        self.move_mouse_to_element(UserGroupLocator.action_list)
        time.sleep(2)
        self.save_screen(doc+'-打开action')
        self.wait_elevisble(UserGroupLocator.action_delete,doc)
        self.click_element(UserGroupLocator.action_delete,doc)
        time.sleep(5)
        self.click_element(UserGroupLocator.delete_acknowledge,doc)
        time.sleep(2)
        self.click_element(UserGroupLocator.delete_confirm,doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)

    #编辑用户组
    def edit_user_group(self,cmdopt):
        doc='编辑用户组'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_usergroup_page(cmdopt)
        time.sleep(3)
        self.wait_elevisble(UserGroupLocator.action_list,doc)
        self.click_element(UserGroupLocator.action_list,doc)
        self.wait_elevisble(UserGroupLocator.action_edit,doc)
        self.click_element(UserGroupLocator.action_edit,doc)
        time.sleep(2)
        self.wait_elevisble(UserGroupLocator.user_group_edit_name,doc)
        self.click_element(UserGroupLocator.user_group_edit_name, doc)
        self.keyboard_event_all_select(UserGroupLocator.user_group_edit_name, doc)
        self.keyboard_event(UserGroupLocator.user_group_edit_name,doc)
        time.sleep(5)
        self.send_keys(UserGroupLocator.user_group_name_edit,UserGroup.deit_user_group_name,doc)
        time.sleep(2)
        self.click_element(UserGroupLocator.delete_confirm,doc)
        time.sleep(3)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)
        self.query_user_group(UserGroup.deit_user_group_name)
        self.click_query_button()
        edit_group_name=self.get_user_group_text()
        return edit_group_name


    def remove_user(self, usergroup, cmdopt):
        doc = '删除用户组下面的用户'
        max_attempts = 3
        attempts = 0
        while attempts < max_attempts:
            try:
                self.refresh_page()
                self.enter_the_usergroup_page(Commondata.home_page[cmdopt]['user'])
                self.query_user_group(usergroup)
                eles = self.get_elements(UserGroupLocator.remove_user, doc)
                if len(eles) == 0:
                    break
                else:
                    for ele in eles:
                        if ele.text == 'Remove User':
                            ele.click()
                            self.wait_elevisble(UserGroupLocator.delete_acknowledge, doc)
                            self.click_element(UserGroupLocator.delete_acknowledge, doc)
                            time.sleep(2)
                            self.click_element(UserGroupLocator.delete_confirm, doc)
                            self.wait_elevisble(OrganizationLocator.add_success_prompt, doc)
            except  StaleElementReferenceException:
                attempts += 1






    def get_user_elements(self):
        doc='获取用户组下面的user元素'
        time.sleep(2)
        eles=self.get_elements(UserGroupLocator.remove_user,doc)
        return eles



    def  add_user_to_group(self,org_name,cmdopt):
        doc='将用户加入用户组'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_usergroup_page(cmdopt)
        self.wait_elevisble(UserGroupLocator.add_user_group,doc)
        self.click_element(UserGroupLocator.add_user_group,doc)
        create_user_groupname = CommonTool.generate_string()
        print(create_user_groupname)
        self.wait_elevisble(UserGroupLocator.user_group_name,doc)
        self.send_keys(UserGroupLocator.user_group_name, create_user_groupname,doc)
        self.wait_elevisble(UserGroupLocator.user_select,doc)
        self.click_element(UserGroupLocator.user_select,doc)
        time.sleep(3)
        self.send_keys(UserGroupLocator.user_select_input,org_name,doc)
        time.sleep(2)
        self.click_create_user()
        time.sleep(2)
        self.click_element(UserGroupLocator.user_group_name,doc)
        time.sleep(4)
        self.click_element(UserGroupLocator.delete_confirm,doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)
        return  create_user_groupname


    def click_create_user(self):
        doc='点击创建的user'
        eles=self.get_elements(UserGroupLocator.user_click,doc)
        eles[-1].click()
        time.sleep(3)







